package com.app.micaihu.view.army.activity

import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import com.app.base.BaseMvpActivity
import com.app.http.bean.BaseBean
import com.app.micaihu.R
import com.app.micaihu.configure.Config
import com.app.micaihu.view.army.contract.ArmyDetailContract
import com.app.micaihu.view.army.presenter.ArmyDetailPresenter
import com.blankj.utilcode.util.BarUtils
import com.blankj.utilcode.util.ToastUtils
import com.jeremyliao.liveeventbus.LiveEventBus
import kotlinx.android.synthetic.main.army_join_activity.*

/**
 * 申请加入军团
 */
class ArmyJoinActivity : BaseMvpActivity<ArmyDetailContract.View, ArmyDetailPresenter>(), ArmyDetailContract.View {
    override fun getPresenter() = ArmyDetailPresenter()

    override fun getLayoutId() = R.layout.army_join_activity


    override fun getBundle(bundle: Bundle) {
        gid = bundle.getString(Config.Extra.id, "")
    }

    private var gid = ""

    override fun setData() {
        BarUtils.addMarginTopEqualStatusBarHeight(rlTitle)
    }

    override fun addListeners() {
        ivBack.setOnClickListener { finish() }
        tvJoin.setOnClickListener {
            val msg = etContent.text.toString()
            if (msg.isEmpty()) {
                ToastUtils.showShort("请输入申请理由")
            } else {
                mPresenter?.join(gid, etContent.text.toString())
            }
        }
        etContent.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            }

            override fun afterTextChanged(s: Editable?) {
                tvLimit.text = "${etContent.text.length}/20字"
            }

        })
    }

    override fun handleJoin(result: BaseBean<Any?>?) {
        if (result == null) {
            ToastUtils.showShort(getString(R.string.net_error))
        } else {
            if (result.isSuccess()) {
                LiveEventBus.get(Config.EventBus.joinArmySuccess).post(true)
                finish()
            }
            ToastUtils.showShort(result.msg)
        }
    }

}